package com.jzo2o.foundations.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {
    @Resource
    private ServeItemMapper serveItemMapper;
    @Resource
    private ServeMapper serveMapper;
    @Resource
    private RegionMapper regionMapper;
    /**
     * 区域服务分页查询
     *
     * @param
     * @return
     */
    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO servePageQueryReqDTO) {
        return PageHelperUtils.selectPage(servePageQueryReqDTO,
                () -> baseMapper.queryList(servePageQueryReqDTO.getRegionId()));
    }

    /**
     * 新增区域服务
     *
     * @param serveUpsertReqDTOList
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(List<ServeUpsertReqDTO> serveUpsertReqDTOList) {
        // 校验数据是否正确
        for (ServeUpsertReqDTO serveUpsertReqDTO : serveUpsertReqDTOList) {
            // 判断服务项是否是启用状态，若不是启用状态就不能新增
            ServeItem serveItem = serveItemMapper.selectById(serveUpsertReqDTO.getServeItemId());
            if (ObjectUtil.isNull(serveItem)) {
                throw new ForbiddenOperationException("服务项不存在，新增失败");
            }
            if (serveItem.getActiveStatus() != 2) {
                throw new ForbiddenOperationException("该服务项未启用无法添加到区域下使用");
            }
            // 判断新增的服务是否重复
            QueryWrapper<Serve> wrapper = new QueryWrapper<Serve>()
                    .eq("region_id", serveUpsertReqDTO.getRegionId())
                    .eq("serve_item_id", serveUpsertReqDTO.getServeItemId());
            Integer count = serveMapper.selectCount(wrapper);
            if (count > 0) {
                throw new ForbiddenOperationException(serveItem.getName()+"服务已存在");
            }
        }
        // 当添加的数据没有问题时，遍历集合添加数据到数据库
        for (ServeUpsertReqDTO serveUpsertReqDTO : serveUpsertReqDTOList) {
            // 新增服务
            Serve serve = BeanUtil.toBean(serveUpsertReqDTO, Serve.class);
            // 补全数据
            // 通过区域id获取城市名字
            Region region = regionMapper.selectById(serveUpsertReqDTO.getRegionId());
            serve.setCityCode(region.getCityCode());
            baseMapper.insert(serve);
        }
    }

    /**
     * 区域服务价格修改
     *
     * @param id
     * @param price
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Serve update(Long id, BigDecimal price) {
        QueryWrapper<Serve> wrapper = new QueryWrapper<Serve>()
                .eq("id", id);
        Serve serve = new Serve();
        serve.setPrice(price);
        boolean result = this.update(serve, wrapper);
        if (!result) {
            throw new CommonException("修改服务价格失败");
        }
        return baseMapper.selectById(id);
    }

    /**
     * 区域服务上架
     *
     * @param id
     */
    @Override
    public void onSale(Long id) {
        // 先根据id查询出当前的服务
        Serve serve = serveMapper.selectById(id);
        if (ObjectUtil.isEmpty(serve)) {
            throw new ForbiddenOperationException("未查询到该服务，上架失败！");
        }
        // 判断当前服务状态是否为草稿或下架
        if (serve.getSaleStatus()!=0 && serve.getSaleStatus()!=1) {
            throw new ForbiddenOperationException("服务状态异常，上架失败！");
        }
        ServeItem serveItem = serveItemMapper.selectById(serve.getServeItemId());
        // 判断服务项是否存在
        if (ObjectUtil.isEmpty(serveItem)) {
            throw new ForbiddenOperationException("未查询到该服务项，上架失败！");
        }
        // 判断服务项是否启用
        if (serveItem.getActiveStatus()!=2) {
            throw new ForbiddenOperationException("服务项未启动，上架失败！");
        }
        // 将服务状态修改为上架
        serve.setSaleStatus(2);
        try {
            serveMapper.updateById(serve);
        } catch (Exception e) {
            throw new CommonException("启动服务失败");
        }
    }

    /**
     * 删除区域服务
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        // 先查询出服务
        Serve serve = serveMapper.selectById(id);
        if (ObjectUtil.isEmpty(serve)) {
            throw new ForbiddenOperationException("未查询到该服务，删除失败！");
        }
        // 判断服务状态是否为草稿
        if (serve.getSaleStatus()!=0) {
            throw new ForbiddenOperationException("服务状态不为草稿，不能删除！");
        }
        // 完成删除
        baseMapper.deleteById(id);
    }

    /**
     * 区域服务下架
     *
     * @param id
     */
    @Override
    public void offSale(Long id) {
        // 先根据id查询出当前的服务
        Serve serve = serveMapper.selectById(id);
        if (ObjectUtil.isEmpty(serve)) {
            throw new ForbiddenOperationException("未查询到该服务，下架失败！");
        }
        // 判断当前服务状态是否为上架
        if (serve.getSaleStatus()!=2) {
            throw new ForbiddenOperationException("服务状态异常，下架失败！");
        }
        ServeItem serveItem = serveItemMapper.selectById(serve.getServeItemId());
        // 判断服务项是否存在
        if (ObjectUtil.isEmpty(serveItem)) {
            throw new ForbiddenOperationException("未查询到该服务项，下架失败！");
        }
        // 判断服务项是否禁用
        if (serveItem.getActiveStatus()!=1) {
            throw new ForbiddenOperationException("服务项未禁用，下架失败！");
        }
        // 将服务状态修改为上架
        serve.setSaleStatus(2);
        try {
            serveMapper.updateById(serve);
        } catch (Exception e) {
            throw new CommonException("启动服务失败");
        }
    }

    /**
     * 区域服务设置热门
     *
     * @param id
     */
    @Override
    public void onHot(Long id) {
        // 先根据id查询出当前的服务
        Serve serve = serveMapper.selectById(id);
        if (ObjectUtil.isEmpty(serve)) {
            throw new ForbiddenOperationException("未查询到该服务，设置热门失败！");
        }
        // 判断当前服务状态是否为上架
        if (serve.getSaleStatus()!=2) {
            throw new ForbiddenOperationException("服务状态异常，设置热门失败！");
        }
        ServeItem serveItem = serveItemMapper.selectById(serve.getServeItemId());
        // 判断服务项是否存在
        if (ObjectUtil.isEmpty(serveItem)) {
            throw new ForbiddenOperationException("未查询到该服务项，设置热门失败！");
        }
        // 判断服务项是否启用
        if (serveItem.getActiveStatus()!=2) {
            throw new ForbiddenOperationException("服务项未启用，设置热门失败！");
        }
        // 判断当前服务是否为非热门
        if (serve.getIsHot()!=1) {
            throw new ForbiddenOperationException("该服务已经为热门，设置热门失败！");
        }
        // 修改热门状态
        serve.setIsHot(1);
        try {
            serveMapper.updateById(serve);
        } catch (Exception e) {
            throw new CommonException("启动服务失败");
        }

    }

    /**
     * 区域服务取消热门
     *
     * @param id
     */
    @Override
    public void offHot(Long id) {
        // 先根据id查询出当前的服务
        Serve serve = serveMapper.selectById(id);
        if (ObjectUtil.isEmpty(serve)) {
            throw new ForbiddenOperationException("未查询到该服务，取消热门失败！");
        }
        // 判断当前服务状态是否为上架
        if (serve.getSaleStatus()!=2) {
            throw new ForbiddenOperationException("服务状态异常，取消热门失败！");
        }
        ServeItem serveItem = serveItemMapper.selectById(serve.getServeItemId());
        // 判断服务项是否存在
        if (ObjectUtil.isEmpty(serveItem)) {
            throw new ForbiddenOperationException("未查询到该服务项，取消热门失败！");
        }
        // 判断服务项是否启用
        if (serveItem.getActiveStatus()!=2) {
            throw new ForbiddenOperationException("服务项未启用，取消热门失败！");
        }
        // 判断当前服务是否为热门
        if (serve.getIsHot()==0) {
            throw new ForbiddenOperationException("该服务并非热门，取消热门失败！");
        }
        // 修改热门状态为非热门
        serve.setIsHot(0);
        try {
            serveMapper.updateById(serve);
        } catch (Exception e) {
            throw new CommonException("启动服务失败");
        }
    }
}
